<html>
  <head>
    <title>组件的生命周期函数(旧)</title>
  </head>
  <body>
    <div id="root"></div>
    <script type="text/javascript" src="../../js/react.development.js"></script>
    <script type="text/javascript" src="../../js/react-dom.development.js"></script>
    <script type="text/javascript" src="../../js/babel.min.js"></script>
    <script type="text/babel">
      class Count extends React.Component {
        constructor(props) {
          super(props);
          this.state = { count: 0 };
        }

        componentWillMount() {
          console.log('Count---componentWillMount');
        }

        componentDidMount() {
          console.log('Count---componentDidMount');
        }

        componentWillUnmount() {
          console.log('Count---componentWillUnmount');
        }

        componentWillReceiveProps(newProps) {
          console.log('Count---componentWillReceiveProps')
        }

        shouldComponentUpdate() {
          console.log('Count---shouldComponentUpdate');
          return true;
        }

        componentWillUpdate() {
          console.log('Count---componentWillUpdate');
        }

        componentDidUpdate() {
          console.log('Count---componentDidUpdate');
        }

        add = () => {
          const { count } = this.state;
          this.setState({ count: count + 1 });
        }

        unmount = () => {
          ReactDOM.unmountComponentAtNode(document.getElementById('root'));
        }

        force = () => {
          this.forceUpdate();
        }

        render() {
          console.log('Count---render');
          const { count } = this.state;

          return (
            <div>
              当前值：{count} <br />
              <button onClick={this.add}>点击加1</button> <br />
              <button onClick={this.unmount}>卸载组件</button> <br />
              <button onClick={this.force}>不更新state，强制更新</button>
            </div>
          );
        }
      }

      class Parent extends React.Component {
        state = { name: 'Judith' }

        change = () => {
          this.setState({ name: 'ZhangSan' });
        }

        render() {
          console.log('Parent---render');
          return (
            <div>
              我是Parent组件 <br />
              <button onClick={this.change}>换名字</button>
              <Child />
            </div>
          );
        }
      }

      class Child extends React.Component {
        render() {
          console.log('Child---render');
          return (
            <div>
              我是Child组件 <br />
            </div>
          );
        }
      }
      ReactDOM.render(<Parent />, document.getElementById('root'));
    </script>
  </body>
</html>
